import {observable, action, autorun, computed} from 'mobx'
import http from 'src/helper/http'
const cnodePath = function(path) {
  return `https://cnodejs.org/api/v1${path}`
}
export default class {
  @observable list = {
    data: [],
    load: false,
    page: 1
  }
  @observable item = {}

  @action async getList(tag = '', limit = 10, mdrender = true) {
    console.log(`getList`)
    if (this.list.load) return
    this.list.load = true
    const {success, data} = await http.get(cnodePath('/topics'), {page: this.list.page, tag, limit, mdrender})
    this.list.load = false
    if (success) {
      this.list.data = this.list.data.concat(data)
      this.list.page++
    }
  }
  @action async refresh(tag = '', limit = 10, mdrender = true) {
    console.log(`refresh`)
    this.list.page = 1
    const {success, data} = await http.get(cnodePath('/topics'), {page: this.list.page, tag, limit, mdrender})
    if (success) {
      this.list.data = data
      this.list.page++
    }
  }
  @action setItem(item = {}) {
    this.item = item
  }
}
